//
//  MCTabViewController.m
//  MClient
//
//  Created by apple_cyg on 10-11-12.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCTabBarController.h"
#import "MCTabBarNavigationController.h"
#import "LoginController.h"
#import "SettingController.h"
#import "AccountController.h"
#import "AboutController.h"
#import "DemoController.h"
#import "MenuController.h"
#import "UIImageAdditions.h"
#import "MCFunction.h"
#import "MenuController.h"

@interface MCTabBarController()
- (UITabBarItem*)createTabBarItem:(NSString*)aTitle withIcon:(NSString*)aIcon withTag:(NSInteger)tag;
@end

@implementation MCTabBarController

- (NSDictionary*)tabBarData {
	return _tabBarData;
}
- (void)setTabBarData:(NSDictionary *)tabBarData {
	[_tabBarData release];_tabBarData=nil;
	_tabBarData = [tabBarData retain];
	
	int i = 0;
	NSMutableArray* viewArray = [NSMutableArray array];
	NSMutableArray* menuArray = [NSMutableArray array];
	NSArray* shortKeyArray = [tabBarData objectForKey:@"sct"];
	BOOL homeAdded = NO;
	for (NSDictionary* item in shortKeyArray) {
		
		if ([viewArray count] == 4) {
			[menuArray addObject:item];
			continue;
		}
		
		NSString* icon = [item objectForKey:@"icon"];
		if (icon == nil || [icon length] == 0) {
			icon = @"0.png";
		}
		NSString* title = [item objectForKey:@"label"];
		
		UITabBarItem* tabBarItem = nil;
		
		MCTabBarNavigationController* navigationController = nil;
		if (!homeAdded && [tabBarData objectForKey:@"app"]) {
			tabBarItem = [self createTabBarItem:@"首页" withIcon:@"Home.png" withTag:i];
			MenuController *menuViewController = [[MenuController alloc] init];
			menuViewController.title = [MCFunction getAppName];
			menuViewController.menus = [tabBarData objectForKey:@"app"];
			menuViewController.isApp = YES;
			menuViewController.tabBarItem = tabBarItem;
			navigationController = [[MCNavigationController alloc] initWithRootViewController:menuViewController];
			[menuViewController release];
			
			homeAdded = YES;
			navigationController.tabBarItem = tabBarItem;
			[tabBarItem release];
			[viewArray addObject:navigationController];
			[navigationController release];
			i++;
			
			goto NO_HOME;
		} else {
		NO_HOME:
			tabBarItem = [self createTabBarItem:title withIcon:icon withTag:i];
			UIViewController *viewController = [[UIViewController alloc] init];
			viewController.title = self.title;
			viewController.tabBarItem = tabBarItem;
			
			if (!homeAdded) {
				UIBarButtonItem *button = [[UIBarButtonItem alloc]
										   initWithTitle:@"返回"
										   style:UIBarButtonItemStyleBordered 
										   target:self
										   action:@selector(goBack)];
				viewController.navigationItem.leftBarButtonItem = button;
				[button release]; button = nil;
			}
			
			navigationController = [[MCTabBarNavigationController alloc] initWithRootViewController:viewController];
			navigationController.menuData = item;
			navigationController.needBackBtn = !homeAdded;
			if (i == 0) {
				[navigationController onRequestMenu];
			}
			[viewController release];
		}

		navigationController.tabBarItem = tabBarItem;
		[tabBarItem release];
		[viewArray addObject:navigationController];
		[navigationController release];
		i++;
	}
	if ([menuArray count] > 0) {

		MenuController *moreViewController = [[MenuController alloc] init];
		moreViewController.title = @"更多";
		moreViewController.menus = menuArray;
		moreViewController.isApp = NO;
		moreViewController.isShortcutMore = YES;
		
//		UIBarButtonItem *button = [[UIBarButtonItem alloc]
//								   initWithTitle:@"返回"
//								   style:UIBarButtonItemStyleBordered 
//								   target:self
//								   action:@selector(goBackAndLoginout)];
//		moreViewController.navigationItem.leftBarButtonItem = button;
//		[button release]; button = nil;
		
		MCNavigationController* navigationController = [[MCNavigationController alloc] initWithRootViewController:moreViewController];
		[moreViewController release];
		UITabBarItem* tabBarItem = [self createTabBarItem:@"更多" withIcon:@"TabMoreProducts.png" withTag:5];
		navigationController.tabBarItem = tabBarItem;
		[tabBarItem release];
		[viewArray addObject:navigationController];
		[navigationController release];
	}
	self.viewControllers = viewArray;
}

- (UITabBarItem*)createTabBarItem:(NSString*)aTitle withIcon:(NSString*)aIcon withTag:(NSInteger)tag {
	UIImage* icon = [[UIImage imageNamed:aIcon] scaleToSize:CGSizeMake(30, 30)];
	return [[UITabBarItem alloc] initWithTitle:aTitle image:icon tag:tag];
}

- (void)viewDidLoad {
	[super viewDidLoad];
	self.delegate = self;
	self.hidesBottomBarWhenPushed = YES;
	self.moreNavigationController.navigationBar.tintColor = [MCUserDefaults instance].skin_navigationBarColor;
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	self.navigationController.navigationBarHidden = YES;
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
	if (self.selectedViewController && [self.selectedViewController isKindOfClass:[UINavigationController class]]) {
		UINavigationController* navigation = (UINavigationController*)self.selectedViewController;
		UIViewController* viewController = navigation.visibleViewController;
		return [viewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
	}

    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


- (void)dealloc {
	[_tabBarData release];
    [super dealloc];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
	if (self.selectedViewController == viewController) {
		return NO;
	} else {
		return YES;
	}
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
	if (![viewController isKindOfClass:[UINavigationController class]]) {
		return;
	}

	[((UINavigationController*)viewController) popToRootViewControllerAnimated:NO];
	
	if ([viewController isKindOfClass:[MCTabBarNavigationController class]]) {
		[(MCTabBarNavigationController*)viewController onRequestMenu];
	}
}

- (void)goBack {
	self.navigationController.navigationBarHidden = NO;
	((MCTabBarNavigationController*)self.selectedViewController).navigationBar.hidden = YES;
	[self.navigationController popViewControllerAnimated:YES];
}

@end
